package com.janushuang.algorithm.sorter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhijing.huang
 *         Created by zhijing.huang on 2014/9/26.
 */
public class InsertSorter<E extends Comparable<E>> extends Sorter<E> {
    private static final Logger LOG = LoggerFactory.getLogger(InsertSorter.class);

    @Override
    public void sort(E[] data, int from, int len) {
        E temp = null;
        for (int i = from+1; i < from+len; i++) {
            temp=data[i];
            int j = i;
            for (;j >from; j--) {
                if (temp.compareTo(data[j-1]) < 0) {
                    data[j] = data[j-1];
                } else {
                    break;
                }
            }
            data[j] = temp;
        }
    }
//    @Override
//    public void sort(E[] data,int) {
//        int loopCount = 0;
//        int len=data.length;
//        for (int i = 1; i < len; i++) {
//            ++loopCount;
////            LOG.debug("i={}",i);
//            for (int j = i; (j>0)&&(data[j]<data[j-1]); j--) {
//                ++loopCount;
////                LOG.debug("j={}",j);
//                SortUtils.swap(data,j,j-1);
////                LOG.debug("data={}",data);
//            }
//        }
////        LOG.info("loopCount={}",loopCount);
//    }

}
